import { deconstructionFn, importFromOrigin, splitMethods, formatThisWithDot } from './common.js'

export const formatComputedItem = (str, section = 'computed') => {
  if (deconstructionFn(str, section)) {
    const { name, params, body, isAsync } = deconstructionFn(str, section)
    return `const ${name} = computed(${isAsync ? 'async ' : ''}(${params})=>{
              ${body
                .split('\n')
                .map((item) => formatThisWithDot(item, section))
                .filter((item) => item)
                .join('\n')}
              })
      `
  }
}
export const formatComputed = (str) => {
  if (!str) return ''
  const formattedStr = str.replace(/^computed:\s?\{([\s\S]*?)\s*?\}$/g, '$1')

  if (formattedStr) {
    importFromOrigin('computed')
    const computedList = splitMethods(formattedStr)
    return computedList.map((item) => formatComputedItem(item)).join('')
  } else {
    return ``
  }
}
